package com.stars.system.fitness.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.RandomUtils;
import com.ruoyi.common.utils.StringUtils;
import com.stars.system.fitness.domain.FitnessRecord;
import com.stars.system.fitness.mapper.FitnessRecordMapper;
import com.stars.system.fitness.service.IFitnessRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * fitnessService业务层处理
 * 
 * @author shuaibizhe
 * @date 2022-06-21
 */
@Service
public class FitnessRecordServiceImpl implements IFitnessRecordService
{
    @Autowired
    private FitnessRecordMapper fitnessMapper;

    private static final String TABLE = "table";

    private static final String CALENDAR = "calendar";

    private static final String COLLAPSE = "collapse";

    /**
     * 查询fitness
     * 
     * @param id fitness主键
     * @return fitness
     */
    @Override
    public FitnessRecord selectFitnessById(Long id)
    {
        return fitnessMapper.selectFitnessById(id);
    }

    /**
     * 定时更新fitness运动数据基线
     *
     * @return fitness
     */
    @Override
    public void updateFitnessBaseline()
    {
//        fitnessMapper.selectFitnessBaseline();
    }

    /**
     * 查询fitness列表
     * 
     * @param fitness fitness
     * @param type  返回类型
     * @return fitness
     */
    @Override
    public List selectFitnessList(FitnessRecord fitness, String type)
    {
        List<FitnessRecord> res = fitnessMapper.selectFitnessList(fitness);
        //表格类型的返回参数
        if (TABLE.equals(type)) {
            return res;
        }
        List<Map> dateList = new ArrayList<>();
        //根据日期先分组
        Map<String, List<FitnessRecord>> dateMap = res.stream()
                                            .collect(Collectors.groupingBy(FitnessRecord::getDate,TreeMap::new,Collectors.toList()))
                                            .descendingMap();

        for (String keyDate : dateMap.keySet()) {
            //根据完成状态再分组
            Map<String, List<FitnessRecord>> statusMap = dateMap.get(keyDate).stream().collect(Collectors.groupingBy(FitnessRecord::getStatus));
            StringBuilder sb = new StringBuilder();
            //拼接日期前缀
            sb.append(keyDate);
            double completionRate = 0;
            for (String keyStatus : statusMap.keySet()) {
                if (COLLAPSE.equals(type)) {
                    // 折叠面板，拼接完成状态信息
                    sb.append("，")
                            .append(DictUtils.getDictLabel("fitness_status", keyStatus))
                            .append("：")
                            .append(statusMap.get(keyStatus).size());
                } else if (CALENDAR.equals(type) && "1".equals(keyStatus)) {
                    // 日历统计完成率
                    completionRate = (Double.valueOf(statusMap.get(keyStatus).size()) / Double.valueOf(dateMap.get(keyDate).size())) * 100;
                }
            }
            double finalCompletionRate = completionRate;
            dateList.add(new HashMap<Object, Object>(4) {
                {
                    put("desc", sb);
                    put("value", dateMap.get(keyDate));
                    put("date", keyDate);
                    put("completionRate", finalCompletionRate);
                }
            });
        }
        return dateList;
    }

    /**
     * 新增fitness
     * 
     * @param fitness fitness
     * @return 结果
     */
    @Override
    public int insertFitness(FitnessRecord fitness)
    {
        fitness.setCreateTime(DateUtils.getNowDate());
        if (StringUtils.isEmpty(fitness.getDate())) {
            fitness.setDate(DateUtils.getDate());
        }
        if (StringUtils.isEmpty(fitness.getStatus())) {
            fitness.setStatus("0");
        }
        return fitnessMapper.insertFitness(fitness);
    }

    /**
     * 修改fitness
     * 
     * @param fitness fitness
     * @return 结果
     */
    @Override
    public int updateFitness(FitnessRecord fitness)
    {
        return fitnessMapper.updateFitness(fitness);
    }

    /**
     * 批量删除fitness
     * 
     * @param ids 需要删除的fitness主键
     * @return 结果
     */
    @Override
    public int deleteFitnessByIds(Long[] ids)
    {
        return fitnessMapper.deleteFitnessByIds(ids);
    }

    /**
     * 删除fitness信息
     * 
     * @param id fitness主键
     * @return 结果
     */
    @Override
    public int deleteFitnessById(Long id)
    {
        return fitnessMapper.deleteFitnessById(id);
    }

    /**
     * 修改fitness完成状态
     *
     * @param fitness fitness
     * @return 结果
     */
    @Override
    public int updateFitnessStatus(FitnessRecord fitness)
    {
        return fitnessMapper.updateFitnessStatus(fitness);
    }
}
